package crm.view.Custom;

import crm.dao.impl.CustomerSqlSeverImpl;
import crm.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 查看消费情况
 */
public class CheckCustomSpending implements ActionListener {
    JFrame frame = null;
    JLabel products = null;
    JLabel id = null;
    JLabel time = null;
    JLabel type = null;
    JLabel price = null;

    JTextField productionField = null;
    JTextField idField = null;
    JTextField timeField = null;
    JTextField typeField = null;
    JTextField priceField = null;


    JButton btn = null;
    JButton btn2 = null;

    JLabel name = null;
    JTextField nameField = null;

    public static void main(String[] args) {
        new CheckCustomSpending();
    }

    public CheckCustomSpending() {
        init();
        frame.setBounds(700, 300, 600, 500);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    public void init() {
        name = new JLabel("姓名:");
        frame = new JFrame("查看消费情况");
        products = new JLabel("所购产品:");
        id = new JLabel("产品编号:");
        time = new JLabel("生产日期:");
        type = new JLabel("生产类型:");
        price = new JLabel("生产价格：");

        btn = new JButton("查询");
        btn2 = new JButton("清除");

        productionField = new JTextField();
        idField = new JTextField();
        nameField = new JTextField();
        timeField = new JTextField();
        typeField = new JTextField();
        priceField = new JTextField();


        name.setBounds(40, 20, 100, 40);
        nameField.setBounds(150, 20, 300, 40);
        btn.setBounds(200, 80, 80, 40);
        btn2.setBounds(300, 80, 80, 40);

        products.setBounds(40, 150, 100, 40);
        productionField.setBounds(150, 150, 300, 40);
        productionField.setEditable(false);
        id.setBounds(40, 200, 100, 40);
        idField.setBounds(150, 200, 300, 40);
        idField.setEditable(false);
        time.setBounds(40, 250, 100, 40);
        timeField.setBounds(150, 250, 300, 40);
        timeField.setEditable(false);
        type.setBounds(40, 300, 100, 40);
        typeField.setBounds(150, 300, 300, 40);
        typeField.setEditable(false);
        price.setBounds(40, 350, 100, 40);
        priceField.setBounds(150, 350, 300, 40);
        priceField.setEditable(false);

        Font font = new Font("宋体", 2, 20);
        btn.setFont(font);
        btn2.setFont(font);
        name.setFont(font);
        nameField.setFont(font);
        products.setFont(font);
        productionField.setFont(font);
        id.setFont(font);
        idField.setFont(font);

        time.setFont(font);
        timeField.setFont(font);
        type.setFont(font);
        typeField.setFont(font);
        price.setFont(font);
        priceField.setFont(font);


        frame.add(products);
        frame.add(productionField);
        frame.add(id);
        frame.add(idField);
        frame.add(btn);
        frame.add(btn2);
        frame.add(name);
        frame.add(nameField);
        frame.add(time);
        frame.add(timeField);
        frame.add(price);
        frame.add(priceField);
        frame.add(type);
        frame.add(typeField);


        btn.addActionListener(this);
        btn2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String text = nameField.getText();
        if ("".equals(text)){
            JOptionPane.showMessageDialog(null,"请输入姓名！");
        }
        else if ("查询".equals(e.getActionCommand())) {
            CustomerSqlSeverImpl dao = new CustomerSqlSeverImpl();
            Customer customer = new Customer();
            List<Customer> getName = dao.getByName(text);
            for (Customer customer1 : getName) {
                productionField.setText(customer1.getProducts());
                idField.setText(customer1.getProduct_id());
                timeField.setText(String.valueOf(customer1.getConsume_time()));
                typeField.setText(String.valueOf(customer1.getProduct_type()));
                timeField.setText(String.valueOf(customer1.getCreate_time()));
                priceField.setText(String.valueOf(customer1.getPrice()));

            }

        }else if ("清除".equals(e.getActionCommand())){
            productionField.setText("");
            nameField.setText("");

        }
    }
}
